package state;

import java.util.Hashtable;

public class GameForm extends Sprite implements AlertListener, KeyListener {
  private Image screen;
  private TimerListener timerListener;
  public boolean isRunning = false;
  
  protected GroupForm parentGroup;
  
  protected GameForm() {
    manager = new Manager(this);
    setPosition(0, 0);
    setSize(GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT);
  }
  
  public final Image getScreen() {
    return screen;
  }
  
  public void setParentGroup(GroupForm parentGroup) {
    this.parentGroup = parentGroup;
  }
  
  public GroupForm getParentGroup() {
    return parentGroup;
  }
  
  public final void setScreen(Image screen) {
    this.screen = screen;
  }

  public final Sprite setTimerListener(TimerListener timerListener) {
    if (this.timerListener != null) {
      GameGlobal.systemCanvas.getTimer().removeTarget(this.timerListener);
      this.timerListener = null;
    }
    GameGlobal.systemCanvas.getTimer().addTarget(timerListener);
    this.timerListener = timerListener;
    return this;
  }
  
  public final Sprite removeTimerListener() {
    if (timerListener != null) {
      GameGlobal.systemCanvas.getTimer().removeTarget(timerListener);
      timerListener = null;
    }
    return this;
  }
  
  public Sprite setPosition(int x, int y) {
    super.setPosition(x, y);
    manager.getBaseSprite().setPosition(x, y);
    return this;
  }

  public Sprite setSize(int width, int height) {
    super.setSize(width, height);
    manager.getBaseSprite().setSize(width, height);
    return this;
  }

  public final void removeScreen() {
    screen = null;
  }
  
  public void init(Hashtable parameters) {}
  
  public void alertEventPerform(int alertType, int eventType, int alertId) {}
  
  public void draw(Graphics g) {}
  
  public void keyPressed(int keyCode){}
  public void keyReleased(int keyCode){}
  public void keyRepeated(int keyCode){}
  
  public void mousePressed(int x, int y){}
  public void mouseReleased(int x, int y){}
  public void mouseMoved(int x, int y){}
  
  public void pointerDragged(int x, int y) {}
  public void pointerPressed(int x, int y) {}
  public void pointerReleased(int x, int y) {}
  
  protected void destroy(){}
  
  public final void doKeyPressed(int keyCode) {
    boolean isMenuShowing = (manager.getBottomBar() != null) && manager.getBottomBar().isMenuShowing();
    manager.doKeyPressed(keyCode);
    if (!isMenuShowing) {
      keyPressed(keyCode);
    }
  }
  
  public final void doKeyReleased(int keyCode) {
    boolean isMenuShowing = (manager.getBottomBar() != null) && manager.getBottomBar().isMenuShowing();
    manager.doKeyReleased(keyCode);
    if (!isMenuShowing) {
      keyReleased(keyCode);
    }
  }
  
  public final void doKeyRepeated(int keyCode) {
    boolean isMenuShowing = (manager.getBottomBar() != null) && manager.getBottomBar().isMenuShowing();
    manager.doKeyRepeated(keyCode);
    if (!isMenuShowing) {
      keyRepeated(keyCode);
    }
  }
  
  public final void doMousePressed(int x, int y) {
    manager.doMousePressed(x, y);
    mousePressed(x, y);
  }
  
  public final void doMouseReleased(int x, int y) {
    manager.doMouseReleased(x, y);
    mouseReleased(x, y);
  }
  
  public final void doMouseMoved(int x, int y) {
    manager.doMouseMoved(x, y);
    mouseMoved(x, y);
  }
  
  public final void doPointerDragged(int x, int y) {
    manager.doPointerDragged(x, y);
    pointerDragged(x, y);
  }
  
  public final void doPointerPressed(int x, int y) {
    manager.doPointerPressed(x, y);
    pointerPressed(x, y);
  }
  
  public final void doPointerReleased(int x, int y) {
    manager.doPointerReleased(x, y);
    pointerReleased(x, y);
  }

  public final void updateFullScreen() {
    manager.updateFullScreen();
  }

  public final void detroyAll() {
    removeScreen();
    isRunning = false;
    removeTimerListener();
    destroy();
    manager.detroyAll();
  }
}
